package com.appbee.yahoo.query.client.ui;

import java.util.List;

import com.appbee.yahoo.query.client.model.Album;
import com.appbee.yahoo.query.client.model.Track;
import com.appbee.yahoo.query.client.text.TrackStrings;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.CellTable.Resources;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
import com.google.gwt.view.client.SingleSelectionModel;

public class TrackTable extends Composite
{

	private static TrackTableUiBinder uiBinder = GWT
			.create(TrackTableUiBinder.class);

	interface TrackTableUiBinder extends UiBinder<Widget, TrackTable>
	{
	}
	
	@UiField(provided=true)
	CellTable<Track> cellTable;
	
	
	@UiField(provided=true)
	SimplePager pager;

	private final SingleSelectionModel<Track> selectionModel = new SingleSelectionModel<Track>();
	private final ListDataProvider<Track> dataProvider = new ListDataProvider<Track>();

	private final TrackStrings trackStrings;

	public TrackTable(int pageSize, Resources res, TrackStrings trackStrings)
	{
		this.trackStrings = trackStrings;
		cellTable = new CellTable<Track>(pageSize,res);
		dataProvider.addDataDisplay(cellTable);
		
		pager = new SimplePager();
		pager.setDisplay(cellTable);
		cellTable.setSelectionModel(selectionModel );
		addColumns();
		initWidget(uiBinder.createAndBindUi(this));
	}
	private void addColumns()
	{
		cellTable.addColumn(new TextCol()
		{
			
			@Override
			public String getValue(Track object)
			{
				return object.getTitle();
			}
		}, trackStrings.title());
		cellTable.addColumn(new TextCol()
		{
			
			@Override
			public String getValue(Track object)
			{
				return object.getArtist()==null?"":object.getArtist().getName();
			}
		}, trackStrings.artist());
		cellTable.addColumn(new TextCol()
		{
			
			@Override
			public String getValue(Track object)
			{
				Album album = object.getAlbum();
				if(album!=null && album.getRelease()!=null)
				{
					return album.getRelease().getTitle();
				}
				return "";
			}
		}, trackStrings.album());
		cellTable.addColumn(new TextCol()
		{
			
			@Override
			public String getValue(Track object)
			{
				return object.getReleaseYear();
			}
		}, trackStrings.getRelaeseYear());
		cellTable.addColumn(new TextCol()
		{
			
			@Override
			public String getValue(Track object)
			{
				return object.getCategory()==null?"":object.getCategory().getName();
			}
		}, trackStrings.genre());
	}
		

	private abstract class TextCol extends Column<Track, String>
	{
		public TextCol()
		{
			super(new TextCell());
		}
		
	}
	
	public void setRowData(List<Track> list)
	{
		dataProvider.setList(list);
	}
	
	public void addSelectListener(Handler handler)
	{
		selectionModel.addSelectionChangeHandler(handler);
	}

	public Track getSelectedTrack()
	{
		return selectionModel.getSelectedObject();
	}
	public void focus()
	{
		Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand()
		{
			
			@Override
			public void execute()
			{
				cellTable.setFocus(true);
			}
		});
	}
}
